package com.larissa.android.quizdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG="MainActivity";
    private static final String KEY_INDEX="currentIdx";
    private static final String KEY_COUNT="count";
    private TextView mQuestionTextView;
    private Button mTrueButton;
    private Button mFalseButton;

    private int mCurrentIdx=0;
    private int mCount=0;
    private Question[] mQuestions=new Question[]{
            new Question(R.string.test_bj,true),
            new Question(R.string.test_dc,false),
            new Question(R.string.test_london,true),
            new Question(R.string.test_tokyo,true)
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG,"onCreate() called");
        mQuestionTextView=(TextView) findViewById(R.id.tx_question);
        mTrueButton=(Button) findViewById(R.id.btn_true);
        mFalseButton=(Button) findViewById(R.id.btn_false);
        mTrueButton.setOnClickListener(this);
        mFalseButton.setOnClickListener(this);

        if(savedInstanceState!=null){
            mCurrentIdx=savedInstanceState.getInt(KEY_INDEX,0);
            mCount=savedInstanceState.getInt(KEY_COUNT,0);
        }
        showQuestion();
    }

    @Override
    public void onClick(View view){
        boolean answer=mQuestions[mCurrentIdx].isQuestionAnswer();
        if(view.getId()==R.id.btn_true){
            if(answer)
                mCount++;
            updateQuestion();
        }
        else if(view.getId()==R.id.btn_false){
            if(!answer)
                mCount++;
            updateQuestion();
        }
    }


    private void updateQuestion(){
        mCurrentIdx++;
        if(mCurrentIdx==mQuestions.length){
            mTrueButton.setEnabled(false);
            mFalseButton.setEnabled(false);
            String toastText=String.format("%s questions are right.",mCount);
            Toast.makeText(MainActivity.this,toastText,Toast.LENGTH_SHORT).show();
        }else{
            showQuestion();
        }
    }
    private void showQuestion(){
        int question;
        if(mCurrentIdx==mQuestions.length){
            mTrueButton.setEnabled(false);
            mFalseButton.setEnabled(false);
            question=mQuestions[mCurrentIdx-1].getQuestionId();
            mQuestionTextView.setText(question);
        }else{
            question=mQuestions[mCurrentIdx].getQuestionId();
            mQuestionTextView.setText(question);
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG,"onSaveInstanceState(Bundle) called");
        savedInstanceState.putInt(KEY_INDEX,mCurrentIdx);
        savedInstanceState.putInt(KEY_COUNT,mCount);
    }

    @Override
    public void onStart(){
        super.onStart();
        Log.d(TAG,"onStart() called");
    }

    @Override
    public void onResume(){
        super.onResume();
        Log.d(TAG,"onResume() called");
    }

    @Override
    public void onPause(){
        super.onPause();
        Log.d(TAG,"onPause() called");
    }

    @Override
    public void onStop(){
        super.onStop();
        Log.d(TAG,"onStop() called");
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy() called");
    }
}